package com.hducms.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;












import com.hducms.cms.dao.CmsCommentDao;
import com.hducms.cms.entity.assist.CmsComment;
import com.hducms.cms.entity.assist.CmsCommentExt;
import com.hducms.cms.entity.main.CmsUser;
import com.hducms.cms.entity.main.Content;
import com.hducms.cms.service.CmsCommentService;
import com.hducms.cms.service.CmsUserService;
import com.hducms.cms.service.ContentService;
//import com.hducms.cms.dao.assist.CmsCommentDao;
//import com.hducms.cms.entity.assist.CmsComment;
//import com.hducms.cms.entity.assist.CmsCommentExt;
//import com.hducms.cms.manager.assist.CmsCommentExtMng;
//import com.hducms.cms.manager.assist.CmsCommentMng;
//import com.hducms.cms.manager.assist.CmsSensitivityMng;
//import com.hducms.cms.manager.main.CmsSiteMng;
//import com.hducms.cms.manager.main.CmsUserMng;
//import com.hducms.cms.manager.main.ContentCountMng;
//import com.hducms.cms.manager.main.ContentMng;
//import com.hducms.common.hibernate3.Updater;
import com.hducms.common.page.Pagination;

@Service
@Transactional
public class CmsCommentServiceImpl implements CmsCommentService {
	@Transactional(readOnly = true)
	public Pagination getPage(Integer contentId,
			Integer greaterThen, Boolean checked, boolean recommend,
			boolean desc, int pageNo, int pageSize) {
//		Pagination page = dao.getPage(contentId, greaterThen, checked,
//				recommend, desc, pageNo, pageSize, false);
		
		Pagination page = new Pagination();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		Map<String, Object> map =  new HashMap<String, Object>();
		if (contentId != null) {
			map.put("contentId", contentId);
		}
		if (checked != null && checked) {
			map.put("checked", checked);
		}
		if (recommend) {
			map.put("recommend", recommend);
		}
		if (desc) {
			map.put("desc", desc);
		}
		map.put("pageSize", pageSize);
		map.put("start", page.getFirstResult());
		
		int totalCount = dao.countQueryResult(map);
		page.setTotalCount(totalCount);

		if (totalCount < 1) {
			page.setList(new ArrayList());
			return page;
		}
		if (totalCount<pageSize) {
			map.put("pageSize", totalCount);
		}
		
		List<CmsComment> list = dao.getPage(map);
		for (CmsComment cmsComment : list) {
			Content content = contentMng.findById(cmsComment.getContentId());
			cmsComment.setContent(content);
			CmsUser cmsUser = cmsUserMng.findById(cmsComment.getCommentUserId());
			cmsComment.setCommentUser(cmsUser);
		}
		
		page.setList(list);	
		
		return page;
	}

//	@Transactional(readOnly = true)
//	public Pagination getPageForTag(Integer siteId, Integer contentId,
//			Integer greaterThen, Boolean checked, boolean recommend,
//			boolean desc, int pageNo, int pageSize) {
//		Pagination page = dao.getPage(siteId, contentId, greaterThen, checked,
//				recommend, desc, pageNo, pageSize, true);
//		return page;
//	}
//	public Pagination getPageForMember(Integer siteId, Integer contentId,Integer toUserId,Integer fromUserId,
//			Integer greaterThen, Boolean checked, Boolean recommend,
//			boolean desc, int pageNo, int pageSize){
//		Pagination page = dao.getPageForMember(siteId, contentId,toUserId,fromUserId, greaterThen, checked,
//				recommend, desc, pageNo, pageSize, false);
//		return page;
//	}
//	
//	@Transactional(readOnly = true)
//	public List<CmsComment> getListForDel(Integer siteId, Integer userId,Integer commentUserId,String ip){
//		return dao.getListForDel(siteId,userId,commentUserId,ip);
//	}
//
//	@Transactional(readOnly = true)
//	public List<CmsComment> getListForTag(Integer siteId, Integer contentId,
//			Integer greaterThen, Boolean checked, boolean recommend,
//			boolean desc, int count) {
//		return dao.getList(siteId, contentId, greaterThen, checked, recommend,
//				desc, count, true);
//	}
//
	@Transactional(readOnly = true)
	public CmsComment findById(Integer id) {
		CmsComment entity = dao.findById(id);
		return entity;
	}
//
//	public CmsComment comment(String text, String ip, Integer contentId,
//			Integer siteId, Integer userId, boolean checked, boolean recommend) {
//		CmsComment comment = new CmsComment();
//		comment.setContent(contentMng.findById(contentId));
//		comment.setSite(cmsSiteMng.findById(siteId));
//		if (userId != null) {
//			comment.setCommentUser(cmsUserMng.findById(userId));
//		}
//		comment.setChecked(checked);
//		comment.setRecommend(recommend);
//		comment.init();
//		dao.save(comment);
//		text = cmsSensitivityMng.replaceSensitivity(text);
//		cmsCommentExtMng.save(ip, text, comment);
//		contentCountMng.commentCount(contentId);
//		return comment;
//	}
//
	public void update(CmsComment bean) {
//		Updater<CmsComment> updater = new Updater<CmsComment>(bean);
//		bean = dao.updateByUpdater(updater);
//		cmsCommentExtMng.update(ext);
//		return bean;
		dao.update(bean);
	}
	
	public void updateExt(CmsCommentExt bean) {
//		Updater<CmsComment> updater = new Updater<CmsComment>(bean);
//		bean = dao.updateByUpdater(updater);
//		cmsCommentExtMng.update(ext);
//		return bean;
		dao.updateExt(bean);
	}
//
//	public int deleteByContentId(Integer contentId) {
//		cmsCommentExtMng.deleteByContentId(contentId);
//		return dao.deleteByContentId(contentId);
//	}

	public void deleteById(Integer id) {

		dao.deleteExtById(id);
		dao.deleteById(id);
		
	}

	public void deleteByIds(Integer[] ids) {
		for (int i = 0, len = ids.length; i < len; i++) {
			deleteById(ids[i]);
		}
	}

//	public void ups(Integer id) {
//		CmsComment comment = findById(id);
//		comment.setUps((short) (comment.getUps() + 1));
//	}
//
//	public void downs(Integer id) {
//		CmsComment comment = findById(id);
//		comment.setDowns((short) (comment.getDowns() + 1));
//	}

	@Override
	public void save(CmsComment bean) {
		dao.save(bean);
	}
	@Override
	public void saveE(CmsCommentExt bean) {
		dao.saveE(bean);
	}
	
	@Override
	public int getTotalCount(int cid) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("contentId", cid);
		param.put("checked", true);
		return dao.countQueryResult(param);
	}

	
//	private CmsSensitivityMng cmsSensitivityMng;
	private CmsUserService cmsUserMng;
//	private CmsSiteMng cmsSiteMng;
	private ContentService contentMng;
//	private ContentCountMng contentCountMng;
//	private CmsCommentExtMng cmsCommentExtMng;
	private CmsCommentDao dao;
//
//	@Autowired
//	public void setCmsSensitivityMng(CmsSensitivityMng cmsSensitivityMng) {
//		this.cmsSensitivityMng = cmsSensitivityMng;
//	}
//
	@Autowired
	public void setCmsUserMng(CmsUserService cmsUserMng) {
		this.cmsUserMng = cmsUserMng;
	}
//
//	@Autowired
//	public void setCmsSiteMng(CmsSiteMng cmsSiteMng) {
//		this.cmsSiteMng = cmsSiteMng;
//	}
//
	@Autowired
	public void setContentMng(ContentService contentMng) {
		this.contentMng = contentMng;
	}

//	@Autowired
//	public void setCmsCommentExtMng(CmsCommentExtMng cmsCommentExtMng) {
//		this.cmsCommentExtMng = cmsCommentExtMng;
//	}
//
//	@Autowired
//	public void setContentCountMng(ContentCountMng contentCountMng) {
//		this.contentCountMng = contentCountMng;
//	}
//
	@Autowired
	public void setDao(CmsCommentDao dao) {
		this.dao = dao;
	}


}